#include <iostream>
#include <algorithm>
using namespace std;
const int N=2*1e5+10;

int n;
int cnt[10]={1,0,0,0,1,0,1,0,2,1};
int a[N];
bool cmp(int a,int b)
{
	int c1,c2;
	int p1=a,p2=b;
	c1=c2=0;
	while(p1)
	{
		c1+=cnt[p1%10];
		p1/=10;
	}
	while(p2)
	{
		c2+=cnt[p2%10];
		p2/=10;
	}
	if(c1==c2)
		return a<b;
	return c1<c2;
}
int main()
{
	cin>>n;
	for(int i=0;i<n;i++)
		cin>>a[i];
	
	
	sort(a,a+n,&cmp);
	for(int i=0;i<n;i++)
	{
		cout<<a[i]<<' ';
	}
	return 0;
}
